package cc.iotkit.data.service;

import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.common.utils.StringUtils;
import cc.iotkit.data.dao.BasAreaRepository;
import cc.iotkit.data.dao.BasMaintenanceUnitRepository;
import cc.iotkit.data.dao.IJPACommData;
import cc.iotkit.data.model.TbBasMaintenanceUnit;
import cc.iotkit.data.system.IBasMaintenanceUnitData;
import cc.iotkit.data.util.PredicateBuilder;
import cc.iotkit.model.bas.BasMaintenanceUnit;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static cc.iotkit.data.model.QTbBasMaintenanceUnit.tbBasMaintenanceUnit;

@Primary
@Service
public class BasMaintenanceUnitImpl implements IBasMaintenanceUnitData, IJPACommData<BasMaintenanceUnit, Long> {
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private BasMaintenanceUnitRepository repository;

    @Override
    public JpaRepository getBaseRepository() {
        return repository;
    }

    @Override
    public Class getJpaRepositoryClass() {
        return TbBasMaintenanceUnit.class;
    }

    @Override
    public Class getTClass() {
        return BasMaintenanceUnit.class;
    }

    public List<BasMaintenanceUnit> findByConditions(BasMaintenanceUnit record) {
        List<TbBasMaintenanceUnit> rets=jpaQueryFactory.select(tbBasMaintenanceUnit).from(tbBasMaintenanceUnit)
                .where(PredicateBuilder.instance()
                        .and(Objects.nonNull(record.getId()),()->tbBasMaintenanceUnit.id.eq(record.getId()))
                        .and(StringUtils.isNotEmpty(record.getUnitName()),()->tbBasMaintenanceUnit.unitName.like("%"+record.getUnitName()+"%"))
                        .build()).fetch();
        return MapstructUtils.copyListToList(rets, BasMaintenanceUnit.class);
    }
}
